package com.wjw.java.io.transformation;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * 字符输出流：
 * 1.把FileOutputStream字节流,转为字符流OutputStreamWriter
 * 2.指定处理的字符编码GBK
 * 3.将OutputStreamWriter传入到BufferedWriter,提高写入效率
 */
public class OutputStreamWriter_ {
    public static void main(String[] args) {
        String filePath = "f:\\IO\\question_code3.txt";
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(filePath),"GBK");
            bw = new BufferedWriter(osw);
            bw.write("你好啊,BufferedWriter!!!");
            bw.newLine();//换行
            bw.write("123456");
            bw.newLine();//换行
            System.out.println("写入数据完成。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //这里只需关闭BufferedWriter,它的底层会去关闭传入的OutputStreamWriter
            try {
                if(bw != null){
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
